package cn.comon.app.scan.service.analysis.impl;

import java.util.Properties;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import cn.comon.app.scan.entity.AsReportinfo;
import cn.comon.app.scan.service.analysis.IAnalysisRequest;
import cn.comon.app.scan.util.UtilTools;

/**
 * 腾讯安全管家软件检测接口
 * @author xujf
 *
 */
@Service("qqRequestImpl")
@Scope("prototype")
@Lazy(value=true)
public class TencentAnalysisRequestImpl implements IAnalysisRequest {
    private static Logger logger = Logger.getLogger(TencentAnalysisRequestImpl.class.getName());
    /**
     * 访问腾讯安全管家的第三方接口
     * @param url 腾讯提供的第三方接口访问地址
     * @param id  腾讯手机管家提供的合作方id
     * @param secureKey 腾讯手机管家提供的密钥
     */
    public Object getSendData(Properties params, Object asReportinfo) {
        try {
            // 腾讯接口数据
            JSONObject sendData = new JSONObject();
            
            // 构造接口访问地址
            String sendUrl = params.getProperty("qq.url") + "?id=" + params.getProperty("qq.channelId");
            sendData.put("sendUrl", sendUrl);
           
            //获取需要检测的软件信息。
            String secureKey = params.getProperty("qq.secureKey");
            AsReportinfo asReportinfoTMP = (AsReportinfo)asReportinfo;
            JSONArray list = new JSONArray();
            JSONObject appData = new JSONObject();
            appData.put("sid", asReportinfoTMP.getId());
            appData.put("url", asReportinfoTMP.getPackageUrl());
            appData.put("md5", asReportinfoTMP.getFileMd5());
            list.add(appData);

            //计算authkey
            String scanParam = list.toString();
            logger.info("【TencentAnalysisRequestImpl.getSendData  scanParam:" + scanParam + "】");
            String authKey = UtilTools.getStringMd5(scanParam + asReportinfoTMP.getId() + secureKey);
            String post = "authkey=" + authKey + "&request=" + scanParam;
            sendData.put("postData", post);
            logger.info("【TencentAnalysisRequestImpl.getSendData  post:" + post + "】");
            return sendData;
       } catch (Exception e) {
            e.printStackTrace();
            logger.error(e);
            return null;
        }
    }
}